package dk.dtu.android.gui;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import dk.dtu.android.transport.R;
import dk.dtu.android.rejseplanen.*;
import dk.dtu.android.transport.gps.*;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

public class DepActivity extends Activity { 
	
	ToggleButton train;
	ToggleButton metro;
	ToggleButton bus;
	
	Spinner spinner;
	ArrayAdapter<CharSequence> spinnerAdapter;
	
	ListView north;
	ListView south;
	
	TextView txtNorth;
	TextView txtSouth;
	
	ArrayList<String> northDep = new ArrayList<String>();
	ArrayList<String> southDep = new ArrayList<String>();
	ArrayList<String> busDep = new ArrayList<String>();
	ArrayList<String> metroDep = new ArrayList<String>();
	
	ArrayList<DepartureBoard> listTrain = new ArrayList<DepartureBoard>();
	ArrayList<DepartureBoard> listBus = new ArrayList<DepartureBoard>();
	ArrayList<DepartureBoard> listMetro = new ArrayList<DepartureBoard>();
	
	ArrayAdapter<String> northAdapter;
	ArrayAdapter<String> southAdapter;
	ArrayAdapter<String> busAdapter;
	ArrayAdapter<String> metroAdapter;
		
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_depart);
                
        txtNorth = (TextView) findViewById(R.id.txtNorth);
        txtSouth = (TextView) findViewById(R.id.txtSouth);
        
        train = (ToggleButton) findViewById(R.id.toggleButton1);
        metro = (ToggleButton) findViewById(R.id.toggleButton2);
        bus = (ToggleButton) findViewById(R.id.toggleButton3);
        
        north = (ListView) findViewById(R.id.listNorth);
        south = (ListView) findViewById(R.id.listSouth);
        
        spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
        
        spinnerAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item);
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(spinnerAdapter);
        
        northAdapter = new ArrayAdapter<String>(this, R.layout.list_item, northDep);
        north.setAdapter(northAdapter);
        southAdapter = new ArrayAdapter<String>(this, R.layout.list_item, southDep);
        south.setAdapter(southAdapter);
        
        train.setChecked(true);
                        
        String x = MyLocationListener.x;
        String y = MyLocationListener.y;
        Log.d("DepActivity", "x: " + x);
        Log.d("DepActivity", "y: " + y);
        
		List<NameValuePair> params = new LinkedList<NameValuePair>();
		params.add(new BasicNameValuePair("coordX", x));
		params.add(new BasicNameValuePair("coordY", y));
		params.add(new BasicNameValuePair("maxRadius", "2000"));
		
        ArrayList<StopLocation> stopsNearby = Rejseplanen.stopsNearby(params);
                
        for (int i = 0; i < stopsNearby.size(); i++) {
        	//Add to spinner
        	spinnerAdapter.add(stopsNearby.get(i).name);
        }
                
        train.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
		        txtNorth.setText("North: ");
		        txtSouth.setText("South: ");
		        
		        train.setChecked(true);
				metro.setChecked(false);
        		bus.setChecked(false);
        		
        		clearLists();
        		listTrainMetro("S", listTrain);
			}
        });
        
        metro.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
		        txtNorth.setText("North: ");
		        txtSouth.setText("South: ");
		        
		        metro.setChecked(true);
				train.setChecked(false);
				bus.setChecked(false);
				
				clearLists();
				listTrainMetro("M", listMetro);
			}        	
        });
        
        
        bus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
		        txtNorth.setText("");
		        txtSouth.setText("");
		        
		        bus.setChecked(true);
				train.setChecked(false);
				metro.setChecked(false);
				
				clearLists();
				listBus();
			}
        });
    }
    
    private class MyOnItemSelectedListener implements OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    		List<NameValuePair> params = new LinkedList<NameValuePair>();
    		params.add(new BasicNameValuePair("coordX", MyLocationListener.x));
    		params.add(new BasicNameValuePair("coordY", MyLocationListener.y));
    		params.add(new BasicNameValuePair("maxRadius", "2000"));

        	ArrayList<StopLocation> stopsNearby = Rejseplanen.stopsNearby(params);

        	listTrain = Rejseplanen.departureBoard(stopsNearby.get(pos).id, null, null, true, false , false);
            listBus = Rejseplanen.departureBoard(stopsNearby.get(pos).id, null, null, false, true , false);
            listMetro = Rejseplanen.departureBoard(stopsNearby.get(pos).id, null, null, false, false , true);

    		clearLists();
    		if(train.isChecked())
    			listTrainMetro("S", listTrain);
    		if(metro.isChecked())
    			listTrainMetro("M", listMetro);
    		if(bus.isChecked())
    			listBus();
        }

        public void onNothingSelected(AdapterView<?> parent) {}
    }

    public void task() {
        Log.d("DepActivity", "listTrain: " + listTrain);
        Log.d("DepActivity", "listBus: " + listBus);
        Log.d("DepActivity", "listMetro: " + listMetro);
        
        if(train.isChecked())
        	listTrainMetro("S", listTrain);
        if(metro.isChecked())
        	listTrainMetro("M", listMetro);
        if(bus.isChecked())
        	listBus();
    }
    
    public void listTrainMetro(String type, ArrayList<DepartureBoard> array) {
        int cntNorth = 0;
        int cntSouth = 0;
        
        for (int i = 0; i < array.size(); i++) {
        	int h = Integer.parseInt(array.get(i).time.substring(0,2));
        	int m = Integer.parseInt(array.get(i).time.substring(3,5));
        	int northSouth = Rejseplanen.northSouth(array.get(i).direction);

        	String mins = getMin2Depart(h,m);
        	
        	if ((type.indexOf(array.get(i).type) >= 0) && (Integer.parseInt(mins) >= 0)) {
        		if (northSouth == 1) {
        			cntNorth++;
        			if(cntNorth < 5) {
        				northDep.add(array.get(i).name + " - " + array.get(i).direction
        						+ " - " + mins + " min.");
        				northAdapter.notifyDataSetChanged();
        			}
        		}
        		if (northSouth == 0) {
        			cntSouth++;
        			if (cntSouth < 5) {
        				southDep.add(array.get(i).name + " - " + array.get(i).direction
        						+ " - " + mins + " min.");
        				southAdapter.notifyDataSetChanged();
        			}
        		}
        	}
        }
    }
    
    public void listBus() {
        int cntBus = 0;
        
    	for (int i = 0; i < listBus.size(); i++) {
        	int h = Integer.parseInt(listBus.get(i).time.substring(0,2));
        	int m = Integer.parseInt(listBus.get(i).time.substring(3,5));

        	String mins = getMin2Depart(h,m);

   			cntBus++;
   			if(Integer.parseInt(mins) >= 0) {
   				northDep.add(listBus.get(i).name + " - " + listBus.get(i).direction
   						+ " - " + mins + " min.");
        		northAdapter.notifyDataSetChanged();
        	}
        }
    }
    
    public String getMin2Depart(int h, int m) {
    	
    	Date now = new Date();
        Calendar cal = GregorianCalendar.getInstance();

        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);

        Date departTime = new GregorianCalendar(year, month,day,h,m).getTime();
        long diff = departTime.getTime() - now.getTime();
        
        String min2Depart = Long.toString(diff/(1000*60));

    	return min2Depart;
    }
    
    public void clearLists() {
    	northDep.clear();
    	southDep.clear();
    	
    	northAdapter.notifyDataSetChanged();
    	southAdapter.notifyDataSetChanged();
    }
}
